package gui.dialogs;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import net.SSHConnectionProxy;
import net.miginfocom.swing.MigLayout;
import core.persistance.Database;
import core.persistance.Profile;

public class Connect extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7200819597130749266L;
	private JTextField txtUsername;
	private JPasswordField pwdPassword;
	private JTextField txtHostname;
	private JTextField txtPort;
	
	private Profile[] profiles = null;
	private JComboBox cmbProfiles;
	
	public Connect() {
		getContentPane().setLayout(new MigLayout("", "[][100px:200px]", "[]10[][][][][]"));
		try {
			Database db = new Database();
			profiles = db.getAllProfiles();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JLabel lblConnectionProfiles = new JLabel("Connection profiles:");
		getContentPane().add(lblConnectionProfiles, "cell 0 0,alignx trailing");
		
		cmbProfiles = new JComboBox(profiles);
		cmbProfiles.setSelectedIndex(-1);
		cmbProfiles.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Profile p = (Profile) cmbProfiles.getSelectedItem();
				setFileds(p);
			}
		});
		getContentPane().add(cmbProfiles, "cell 1 0,growx");
		
		JLabel lblUsername = new JLabel("Username:");
		getContentPane().add(lblUsername, "cell 0 1,alignx trailing");
		
		txtUsername = new JTextField();
		getContentPane().add(txtUsername, "cell 1 1,growx");
		txtUsername.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		getContentPane().add(lblPassword, "cell 0 2,alignx trailing");
		
		pwdPassword = new JPasswordField();
		getContentPane().add(pwdPassword, "cell 1 2,growx");
		
		JLabel lblHostname = new JLabel("Hostname:");
		getContentPane().add(lblHostname, "cell 0 3,alignx trailing");
		
		txtHostname = new JTextField();
		getContentPane().add(txtHostname, "cell 1 3,growx");
		txtHostname.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		getContentPane().add(lblPort, "cell 0 4,alignx trailing");
		
		txtPort = new JTextField();
		getContentPane().add(txtPort, "cell 1 4,growx");
		txtPort.setColumns(10);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Messages about empty fields
				// TODO Port validation
				if (!areTextFiledsFilled())
					return;
				setFields(false);
				SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
					@Override
					protected void done() {
						try {
							get();
							dispose();
							setVisible(false);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ExecutionException e) {
							setFields(true);
							e.printStackTrace();
						}
					}

					@Override
					protected Void doInBackground() throws Exception {
						SSHConnectionProxy.setUp(txtUsername.getText(),
								pwdPassword.getPassword(),
								txtHostname.getText(),
								Integer.parseInt(txtPort.getText()));
						return null;
					}
				};
				worker.execute();
			}
		});
		getContentPane().add(btnConnect, "cell 1 5,alignx right");
	}

	public void setFileds(Profile p) {
		txtUsername.setText(p.getUsername());
		pwdPassword.setText(p.getPassword());
		txtHostname.setText(p.getHostname());
		txtPort.setText(Integer.valueOf(p.getPort()).toString());
	}
	
	public void setFields(boolean b) {
		cmbProfiles.setEnabled(b);
		txtUsername.setEnabled(b);
		pwdPassword.setEnabled(b);
		txtHostname.setEnabled(b);
		txtPort.setEnabled(b);
	}
	
	private boolean areTextFiledsFilled() {
		if (txtUsername.getText().equals(""))
			return beepAndRequestFocus(txtUsername);
		if (pwdPassword.getPassword().equals(""))
			return beepAndRequestFocus(pwdPassword);
		if (txtHostname.getText().equals(""))
			return beepAndRequestFocus(txtHostname);
		if (txtPort.getText().equals(""))
			return beepAndRequestFocus(txtPort);
		return true;
	}
	
	private boolean beepAndRequestFocus(JTextField tf) {
		Toolkit.getDefaultToolkit().beep();
		tf.requestFocusInWindow();
		return false;
	}
}
